package com.rongke.web.api;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rongke.commons.JsonResp;
import com.rongke.commons.PageDto;
import com.rongke.model.EachPicture;
import com.rongke.model.Information;
import com.rongke.service.InformationService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @InformationController
 * @Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value="/api/information")
@Transactional
@CrossOrigin
public class InformationController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private InformationService informationService;

    /**
     * @添加
     * @param information
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/add", method = RequestMethod.POST)
    public JsonResp addInformation(@RequestBody Information information){
        log.debug("添加");
        informationService.insert(information);
        return JsonResp.ok(information);
    }

    /**
     * @修改
     * @param information
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updateInformation(@RequestBody Information information){
        log.debug("修改");
        informationService.updateById(information);
        return JsonResp.ok(information);
    }
    /**
     * @根据id查找
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectOne", method = RequestMethod.GET)
    public JsonResp selectInformation(Long id){
        log.debug("查找");
        Information information = informationService.selectById(id);
        if (information!=null){
            return JsonResp.ok(information);
        }else{
            return JsonResp.fa("未查找到该数据");
        }
    }

    /**
    * 前后台的
   * 咨询查询帮助
   * */
    @RequestMapping(value ="/selectInfolist",method = RequestMethod.POST)
    public JsonResp selectInfolist(Page page, Information information){
        Map map=new HashMap();
        map.put("pageNo",(page.getCurrent()-1)*page.getSize());
        map.put("pageSize",page.getSize());
        map.put("information",information);
        List<Information> informationList=informationService.selectInfolist(map);
       Integer sunm=informationService.selectInfolistCount(map);
        return JsonResp.ok(new PageDto(page.getCurrent(),page.getSize(),informationList,sunm));
    }

   /**
    * 删除
   * */
   @RequestMapping(value ="/delect",method = RequestMethod.POST)
   public JsonResp delect(Integer id){
    Boolean boo=informationService.deleteById(id);
      if(boo==true){
          return JsonResp.ok("删除失败");
      }else{
          return JsonResp.fa("删除失败");
      }

   }

    /**
     * @查找股市资讯
     * @param
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectNewsMsg", method = RequestMethod.GET)
    public JsonResp selectNewsMsg(Integer type){
        log.debug("查找股市资讯");
        EntityWrapper<Information> ew=new EntityWrapper<>();
        ew.eq("type",type);
        ew.orderBy("gmt_datetime",false);
        List<Information> list=informationService.selectList(ew);
        return JsonResp.ok(list);
    }

    @RequestMapping(value ="/delects",method = RequestMethod.GET)
    public JsonResp delects(String str,Integer type){
        Boolean boo=false;
            if (type==2){
                 Information info=new Information();
                 info.setStatus(type);
                 info.setId(Long.valueOf(str));
                boo= informationService.updateById(info);
            }else if (type==1){
                Information info=new Information();
                info.setStatus(type);
                info.setId(Long.valueOf(str));
                boo= informationService.updateById(info);
            }else  if (type==4){
                String [] st=str.split(",");
                List list=new ArrayList<>();
                for (String s:st){
                    list.add(s);
                }
                 boo=informationService.deleteBatchIds(list);
            }

          return JsonResp.ok(boo);
    }


}
